<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Teacher extends Controller
{
    public function teacher()
    {
        // 加载模板
        $this->assign('title', '教师管理');
        return $this->fetch('admin/a_teacher_index');

    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
//        $list = Db::name('teacher')->field(true)->order('id','aes')->select();
        $list = Db::name('teacher')->paginate(3);
//        设置分页
        $page = $list->render();
//        var_dump($list);

//        助手函数
        return view('admin/a_teacher_index', [
            'title' => '教师列表',
            'list' => $list,
            'page' => $page
        ]);

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
//        $info['status'] = true;
//        $info['info'] = '添加成功';
//        return json($info);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function edit($id)
    {

        $list = Db::name('teacher')->field(true)->find($id);
//        var_dump($list);
//        exit;
        $this->assign('list', $list);
        return $this->fetch('admin/a_teacher_edit');
    }


    public function save(Request $request)
    {

        $p = input('post.');
// 依赖注入的方式
        // $p = $request->post();
//         var_dump($p);

        // 处理数据
        $data = [
            'tch_name' => $p['tch_name'],
            'tch_sex' => $p['tch_sex'],
            'tch_age' => $p['tch_age'],
            'tch_phone' => $p['tch_phone'],
            'tch_type' => $p['tch_type'],
            'tch_detail' => $p['tch_detail'],
        ];

//         var_dump($data);
        // 执行新增数据操作
        $result = Db::name('teacher')->data($data)->insert();
        if ($result > 0) {
            return $this->success('修改成功', url('admin/teacher/index'));
        } else {
            return $this->error('修改失败');
        }
    }

    /**
     * 显示指定的资源(框)
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read()
    {

    }

    /**
     * 显示指定的资源(页)
     * @param int $id
     * @return Response
     */
    public function readpage()
    {

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */


    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $info = input('put.');
//        var_dump($info);

        $list = [
            'tch_name' => $info['tch_name'],
            'tch_sex' => $info['tch_sex'],
            'tch_age' => $info['tch_age'],
            'tch_phone' => $info['tch_phone'],
            'tch_type' => $info['tch_type'],
            'tch_detail' => $info['tch_detail']
        ];
        $result = Db::name('teacher')->where('id', $id)->update($list);
        if ($result) {
            return $this->success('修改成功', url('admin/teacher/index'));
        } else {
            return $this->error('未修改');
        }


    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::name('teacher')->delete($id);
        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为:' . $id . '的用户删除成功';

        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为:' . $id . '的用户删除失败';
        }
        return json($info);

    }


}
